#include <iostream>
#include <sstream>

#include "linear_system.h"

using namespace std;


int main() {
  int number_of_lines, number_of_variables;
  string format_code;
  string line;

  cout << "*************************************************" << endl;
  cout << "*       Linear Equations System Solver          *" << endl;
  cout << "*************************************************" << endl;

  cout << endl << endl << "Please, insert your system in the following format: " << endl;
  cout << "<number_of_equations> <number_of_variables>" << endl;
  cout << "<A line for each equation>" << endl;
  cout << endl << endl;
  cout << "An equation should be of the form  <term> ['+' <term>]* = <number>" << endl;
  cout << "Where <term> = <var>|<number> '*' <var>| <var> '*' <number>" << endl << endl;

  getline(cin, line);
  istringstream iss(line);
  iss >> number_of_lines >> number_of_variables;

  if (number_of_lines < 1 or number_of_lines > 100)
    return 0;

  LinearSystem *linear_system
    = LinearSystem::ReadLinearSystemFromStdin(number_of_lines, number_of_variables, format_code);

  if (!linear_system) {
    cout << format_code << endl;
    return 0;
  } else {
    LinearSystem *solved_linear_system = linear_system->Resolve();
    linear_system->PrintSystemToStdout();
    solved_linear_system->PrintSystemToStdout();
    solved_linear_system->PrintSolutionToStdout();
  }

  return 0;
}
